package jdbc;

import com.mysql.cj.jdbc.Driver;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * Description:JDBC连接MySQL的方法
 * User:22954
 * Date:2024-05-11
 * Time:15:10
 */
public class Connection_ {
    @Test
    public void m1() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/java_db01";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "javamysql");
        Connection connection = driver.connect(url, info);
        System.out.println(connection);
        connection.close();
    }

    @Test
    public void m2() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.getDeclaredConstructor().newInstance();
        String url = "jdbc:mysql://localhost:3306/java_db01";
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "javamysql");
        Connection connection = driver.connect(url, info);
        System.out.println(connection);
        connection.close();
    }

    @Test
    public void m3() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) aClass.getDeclaredConstructor().newInstance();
        String url = "jdbc:mysql://localhost:3306/java_db01";
        String user = "root";
        String password = "javamysql";
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        connection.close();
    }

    @Test
    public void m4() throws ClassNotFoundException, SQLException {//最常用
        Class.forName("com.mysql.cj.jdbc.Driver");
        /*
        Driver类加载的时候的静态代码块已注册驱动,可省略手动驱动步骤
            static {
                try {
                    DriverManager.registerDriver(new Driver());
                } catch (SQLException var1) {
                    throw new RuntimeException("Can't register driver!");
                }
            }
        mysql驱动版本大于5.1.6可省略Class.forName()加载Driver类
        jdk会自动调用mysql.jar\\META-INF\\services\\java.sql.Driver文本中的类名去加载,从而自动注册
         */
        String url = "jdbc:mysql://localhost:3306/java_db01";
        String user = "root";
        String password = "javamysql";
        java.sql.Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        connection.close();
    }

    @Test
    public void m5() throws IOException, ClassNotFoundException, SQLException {//最灵活,依赖性最低,最推荐
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/jdbc/mysql.properties"));
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
//        Class.forName(driver);可写可不写
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
        connection.close();
    }
}
